import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { UserService } from '../../../pms/user-list/user.service';
import { DeviceService } from '../../device-list/device.service';
import { DeviceInspectionTemplateService } from '../device-inspection-template.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzTreeNode } from 'ng-zorro-antd/tree';

@Component({
  selector: 'app-device-inspection-template-edit',
  templateUrl: './device-inspection-template-edit.component.html',
  styles: [],
})
export class DeviceInspectionTemplateEditComponent implements OnInit {


  constructor(private formBuilder: FormBuilder,
              private msgSrv: NzMessageService,
              private deviceInspectionTemplateSrv: DeviceInspectionTemplateService,
              private userSrv: UserService,
              private deviceSrv: DeviceService,
  ) {
  }

  @Output() voted = new EventEmitter();

  nzLoading = false;

  validateForm!: FormGroup;

  // 人员
  users = [];
  // 设备
  devices = [];

  visible = false;
  isUpdate = false;
  value?: [];

  open(): void {
    this.isUpdate = false;
    this.visible = true;
  }

  edit(param) {
    this.isUpdate = true;
    this.visible = true;
    this.value = param?.parent?.id;

    if (param) {
      this.validateForm.patchValue(param);
    } else {
      this.validateForm.reset();
    }
  }

  close(): void {
    this.visible = false;
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    this.nzLoading = true;

    this.deviceInspectionTemplateSrv.update(this.validateForm.value).subscribe((res) => {

      this.nzLoading = false;

      if (res.code === 1) {
        this.msgSrv.success('保存完成');
        this.close();
        this.voted.emit();
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  updateConfirmValidator(): void {
    /** wait for refresh value */
    Promise.resolve().then(() => this.validateForm.controls.checkPassword.updateValueAndValidity());
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.validateForm.controls.password.value) {
      return { confirm: true, error: true };
    }
    return {};
  };

  getCaptcha(e: MouseEvent): void {
    e.preventDefault();
  }

  loadUserDate() {
    this.userSrv.getAll().subscribe((res) => {
      if (res.code === 1) {
        this.users = res.data;
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  loadDeviceDate() {
    this.deviceSrv.getAll().subscribe((res) => {
      if (res.code === 1) {
        this.devices = res.data;
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  ngOnInit(): void {
    // 加载人员数据
    this.loadUserDate();
    // 加载设备数据
    this.loadDeviceDate();

    this.validateForm = this.formBuilder.group({
      id: [null],
      code: [null, [Validators.required]],
      name: [null, [Validators.required]],
      enable: [null],
      description: [null],
    });

  }

  nzDisplayWith(node: NzTreeNode) {
    return node.title;
  }

  onChange($event: string): void {
    console.log($event);
  }

}
